home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / optivc16 / cmath.txt < prev    next >
Encoding:
Text File  |  1999-03-06  |  39.3 KB  |  871 lines

  1.     CCCCCCC     MM         MM          A         TTTTTTTTTT    HH      HH
  2.   CC            MMM       MMM         AAA            TT        HH      HH
  3.  CCC            MMMM     MMMM        AA AA           TT        HH      HH
  4.  CCC            MM MM   MM MM       AA   AA          TT        HHHHHHHHHH
  5.  CCC            MM  MM MM  MM      AAAAAAAAA         TT        HH      HH
  6.   CC            MM   MMM   MM     AA       AA        TT        HH      HH
  7.     CCCCCCC     MM    M    MM    AA         AA       TT        HH      HH
  8.  
  9.  
  10.                            CMATH 1.5
  11.  
  12.                     Dr. Martin Sander Software Development
  13.                     Sertürnerstr. 11
  14.                     D-37085 Göttingen
  15.                     Germany
  16.                     e-mail: MartinSander@Bigfoot.com
  17.                     http://www.optivec.com
  18.  
  19. For the commercial version, please order by e-mail or through our web-site!
  20. See chapter 1.3 for details.
  21.  
  22. *****************************************************************************
  23.  
  24. !!     This is an ASCII text file!  It is best viewed with a simple        !!
  25. !!     DOS editor.                                                         !!
  26. !!     If you load this file into a word processor under Windows, you      !!
  27. !!     must use the filter "DOS text".                                     !!
  28. !!     Alternatively, you may use FCONVERT (shipped with Borland C++) to   !!
  29. !!     convert from ASCII (OEM) into the ANSI character set.               !!
  30. !!     preferably use the lettertype CourierNew 10 pt.                     !!
  31.  
  32.                  **************************************
  33.    German-speaking users:
  34.        Um die Kosten für das Herunterladen der Shareware-Version
  35.        über das Internet für alle so gering wie möglich zu halten,
  36.        enthält diese nur die englische Dokumentation. Sie finden
  37.        die deutsche Beschreibung separat unter
  38.                http://www.gwdg.de/~msander/Download/BC/CMDOCD.ZIP
  39.                  **************************************
  40.  
  41. OptiCode (TM) and OptiVec (TM) are trademarks of Dr. Martin Sander
  42. Software Dev.  Other brand and product names mentioned in this handbook
  43. for identification purposes are trademarks or registered trademarks of
  44. their respective holders.
  45.  
  46.  
  47. ****************************************************************************
  48. *                                                                          *
  49. *******                           Contents                           *******
  50. *                                                                          *
  51. ****************************************************************************
  52.  
  53. 1. Introduction
  54.    1.1 What is CMATH ?
  55.    1.2 Licence Terms
  56.    1.3 Registered Versions
  57.  
  58. 2. Getting Started
  59.    2.1 Installation
  60.    2.2 De-Installation
  61.  
  62. 3. Overview over the Functions of CMATH
  63.    3.1 Initialization of Complex Numbers
  64.    3.2 Data-Type Interconversions
  65.    3.3 Basic Complex Operations
  66.    3.4 Arithmetic Operations
  67.    3.5 Mathematical Functions
  68.  
  69. 4. Error Handling
  70.    4.1 General Error Handling of Complex Functions
  71.    4.2 Advanced Error Handling: Writing Messages into a File
  72.  
  73. 5. Syntax Reference
  74.    5.1 C++ Version
  75.    5.2 Plain-C Version
  76.  
  77. ****************************************************************************
  78. *                                                                          *
  79. *******                       1. Introduction                        *******
  80. *                                                                          *
  81. *****************************************************************************
  82.  
  83.  
  84. 1.1 What is CMATH ?
  85. -------------------
  86.  
  87. CMATH is a comprehensive library for complex-number arithmetis and
  88. mathematics. It is primarily intended as a high-quality replacement for
  89. the C++ complex class libraries presently available. In addition,
  90. all functions may be called from C, without the necessity to use C++.
  91. Superior speed, accuracy and safety are achieved through the 
  92. implementation in Assembly language (as opposed to the compiled or 
  93. inline C++ code of the complex class libraries).
  94. Only for the most simple tasks, alternative inline C++ functions are used.
  95.  
  96. The implementation was guided by the following rules:
  97.  
  98.    1. Without any compromise, top priority is always given to the mathema-
  99.       tically correct result, with the accuracy demanded for the respective
  100.       data type. Especially for complex functions, this necessitates a
  101.       very thorough treatment of many different situations. To this end,
  102.       the various cases have to be distinguished with pedantic care.
  103.    2. Mathematical functions must be "safe" under all circumstances.
  104.       They may for no reason simply crash, but have to perform a decent
  105.       error treatment. This is true even - and perhaps especially - for
  106.       seemingly nonsense arguments, with the single exception of the
  107.       non-numbers INF and NAN, which occur themselves only as a result
  108.       of serious errors in other functions.
  109.    3. By all possible means, greatest execution speed must be attained.
  110.       (After all, you did not buy your fast computer for nothing!)
  111.    4. The program code has to be as compact as possible. However, in case
  112.       of conflicts, faster execution speed is always given priority
  113.       over smaller code size.
  114.  
  115. Having a look into the complex class libraries shipped with popular compilers,
  116. you will immediately discover the differences between our approach and theirs.
  117. Often the mathematical functions are implemented by simply writing down the
  118. textbook formula. This yields relatively compact source code. But, due to
  119. round-off error of intermediate results, the final results returned by these
  120. functions are sometimes very inaccurate or even completely wrong. Moreover,
  121. they may lead to unhandled floating-point errors (this means: program crash!).
  122. Unnecessary to mention that the code thus generated is rather slow.
  123.  
  124. For programmers who prefer classis C-style functions over C++, CMATH
  125. provides all complex-number operations and functions also for the language C.
  126. This is accomplished through an additional set of alternative declarations
  127. of the complex types as structs in place of classes.
  128.  
  129. CMATH versions for Pascal and Delphi are also available. As far as possible,
  130. all functions have the same names in the Pascal/Delphi version as in the
  131. C version.
  132. The C++ classes and the C structs are binary compatible with each other.
  133. This point may become important for large projects with mixed C and C++
  134. modules.
  135.  
  136. Existing Borland C++ code which uses the complex class library contained in
  137. <complex.h> can be left unchanged, because the CMATH functions and data
  138. types are also binary compatible with those of <complex.h>.
  139. Here is a detailed description of how to switch from the complex classes of
  140. Borland C++ to the new implementation given by CMATH:
  141.  
  142. *  In C++ modules, replace the statement
  143.        #include <complex.h>
  144.    by the statement
  145.        #include <newcplx.h>
  146.    Then, the following three complex classes are defined:
  147.    class complex<float>,  class complex<double>,
  148.    and  class complex<extended>.
  149.  
  150.    The data types fComplex, dComplex, and eComplex are defined as
  151.    synonyms for these classes.
  152.    In order to avoid the letter "L" (which is already over-used by
  153.    "long int" and "unsigned long" in the language C), the type
  154.    "extended" is used here as a synonym for "long double".
  155.    Consequently, the complex data type consisting of long doubles
  156.    is named "eComplex".
  157.    Thereby, the way is held open for a future inclusion of whole-
  158.    number complex types into CMATH. Then,"liComplex" and "ulComplex"
  159.    will denote the complex types consisting of "long int" or
  160.    "unsigned long" parts, respectively.
  161.  
  162. *  If you prefer to have the "classic" class complex of older releases
  163.    of Borland C++, you have to declare
  164.        #define CMATH_CLASSIC_COMPLEX
  165.    before (!) including <newcplx.h>.
  166.    In this case, only the class complex will be defined and gets the synonym
  167.    dComplex. Here you will have no access to the complex-number functions of
  168.    float and of extended precision.
  169.  
  170. *  For C modules, you cannot include <nexcplx.h>. Rather, please declare
  171.       #include <cmath.h>
  172.    If you are using only one level of floating-point precision, you may
  173.    wish to include only one of the type-specific include-files:
  174.    <cfmath.h>,  <cdmath.h>, or <cemath.h>, respectively.
  175.    The plain-C implementation of CMATH is based upon the following
  176.    definitions of the three complex data types:
  177.       typedef  struct { float     Re, Im; }  fComplex;
  178.       typedef  struct { double    Re, Im; }  dComplex;
  179.       typedef  struct { extended  Re, Im; }  eComplex;
  180.  
  181.    As described above, the data type "extended" is used as a synonym for
  182.    "long double".
  183.  
  184. *  In the C++ classes, the real and imaginary parts are declared as public
  185.    (in contrast to Borland C++ !) and named "Re" and "Im", respectively.
  186.    This allows to access them as "z.Re" and "z.Im"  in C++ modules as well
  187.    as in C modules.
  188.  
  189. This documentation describes the OptiVec implementations for
  190.  
  191. - Borland C++ (Version 3.0 or higher, incl. Borland C++ Builder)
  192.   for DOS and Microsoft Windows 3.0 or later (or Win-OS sessions under
  193.   IBM OS/2 2.0 or later; in the following, we will simply speak
  194.   of "Windows"). The library for the memory model FLAT for Windows95/98 and
  195.   WindowsNT requires Borland C++, version 4.0 or higher.
  196.  
  197. - Microsoft Visual C++ (Version 5.0 or higher)
  198.   for Windows95/98/NT on PC platforms.
  199.  
  200. - Powersoft Optima++ (Version 1.5 or higher)
  201.   for Windows95/98/NT on PC platforms.
  202.  
  203. Please note that only the documentation is valid for these different
  204. compilers. The libraries themselves are compiler-specific; each library
  205. can be used only with one compiler and, in the case of Borland C++,
  206. with one memory model.
  207.  
  208. Borland C++ only:
  209. -----------------
  210.    Depending on your choice when ordering or downloading the Shareware
  211.    version,
  212.    you have got either of the following three library versions:
  213.    memory model FLAT for Windows95/NT,
  214.                 LARGE for DOS, or
  215.                 LARGE for Windows 3.x.
  216.    All of them require, at least, a 386 computer equipped with a 387
  217.    coprocessor. This means: no emulation, no 486SX, but preferably 486DX,
  218.    Pentium or higher.
  219.  
  220.    The full (registered) version contains libraries for all memory models of
  221.    DOS, 16-bit Windows and 32-bit Windows. These libraries, in turn, are
  222.    shipped in three versions:
  223.    one for 486DX and Pentium computers, the second for 386 with 387,
  224.    the third for 286 with or without coprocessor, i.e. with emulation.
  225.  
  226. Microsoft Visual C++ only:
  227. --------------------------
  228.    The Shareware version is for "single-thread debug" and for
  229.    "multi-thread debug". The full (registered) version for Microsoft
  230.    Visual C++ contains additional libraries for "multi-thread DLL",
  231.    single-thread and multi-thread release.
  232.  
  233.  
  234. Versions for other C compilers are in preparation.
  235.  
  236.  
  237. 1.2 Licence Terms
  238. -----------------
  239.  
  240. This is the English Shareware version of CMATH ("SOFTWARE").
  241. It may be used under the following licence terms:
  242.  
  243. 1. You may test the SOFTWARE free of charge for an unlimited period of time.
  244.    This testing phase ends when you permanently integrate functions of this
  245.    SOFTWARE into any of your applications (programs, program parts...).
  246. 2. If you want to use this SOFTWARE for commercial purposes, you have
  247.    to purchase the registered version (see chapter 1.3).
  248. 3. Use of this SOFTWARE for educational purposes at schools and universities
  249.    remains free of charge. However, if any application created under these
  250.    terms is sold to others or otherwise used for commercial purposes,
  251.    paragraph 2 applies.
  252. 4. Distributing this SOFTWARE to others is allowed only in one of the
  253.    following two ways:
  254.    a) linked into your programs, so that the parts stemming from this
  255.       SOFTWARE do no longer appear as a library.
  256.    b) as a whole in unchanged form (in particular the Copyright and Licence
  257.       statements!), whereby you may ask a fee only and exclusively for the
  258.       physical act of copying the SOFTWARE.
  259. 5. This SOFTWARE is provided on an "as is" basis. Any explicit or implicit
  260.    warranties for the SOFTWARE are excluded.
  261.    Despite thorough testing of the SOFTWARE, errors and bugs cannot
  262.    be excluded with certainty. No claims as to merchantability or fitness
  263.    for a particular purpose are made.
  264.    You may not use the SOFTWARE in any environment or situation where
  265.    personal injury or excessive damage to anyone's property (including
  266.    your own) could arise from malfunctioning of the SOFTWARE.
  267.  
  268.  
  269. Copyright for the SOFTWARE and its documentation (C) 1996-1999 Martin Sander
  270.  
  271. All rights reserved, including those of translation into foreign languages.
  272.  
  273. Address of the author:
  274.               Dr. Martin Sander Software Development
  275.               Sertürnerstr. 11
  276.               D-37085 Göttingen
  277.               Germany
  278.               e-mail: MartinSander@Bigfoot.com
  279.  
  280.  
  281.  
  282. 1.3 Registered Versions
  283. -----------------------
  284.  
  285. In order to make this product affordable also for those who will not
  286. themselves make money using it, we offer an "educational edition" at a
  287. strongly reduced rate, in addition to the full "commercial edition".
  288. The contents of these two editions is identical. The only difference lies
  289. in the restrictions of use: The "educational edition" may not be used for
  290. commercial / business / government purposes, but is restricted to private
  291. and educational use.
  292.  
  293. Purchasing the full (registered) version gives you the right to use it on
  294. as many computers at a time as the number of units you bought.
  295. Corporate site and world-wide licences are available upon request.
  296.  
  297. The full version (both the commercial and the educational editions)
  298. of CMATH for Borland C++ and of CMATH for Microsoft Visual C++
  299.  
  300. -  support all memory models of Windows95/98, NT, 3.x, and DOS
  301.    (Borland C++)
  302.    or "single-thread", "multi-thread", "multi-thread DLL", both
  303.    debug and release  (Microsoft Visual C++)
  304.  
  305. -  (Borland C++ only: )
  306.    have individually optimized libraries for each degree of processor
  307.    backward-compatibility:
  308.       486DX/Pentium+ (optimized for Pentium/PentiumPro)
  309.       386+ (387 coprocessor required)
  310.       286+ (no coprocessor required).
  311.  
  312. -  come with printed documentation.
  313.  
  314. -  entitle you to two free updates.
  315.  
  316. -  can be ordered at the following conditions:
  317.  
  318.    a) if you can pay in German Marks (from 1999 on: in Euro)
  319.       and order directly from the author, the price is
  320.       DM  59,- for  1 unit of the educational edition
  321.       DM  99,- for  1 unit of the commercial edition,
  322.       DM 350,- for  5 units,
  323.       DM 600,- for 10 units    (incl. 16% VAT, plus DM 10,- handling charge).
  324.       Please order by sending an e-mail to MartinSander@bigfoot.com
  325.       or use a print-out of the file ORDER.TXT.
  326.       Payment options:
  327.           - pre-paid by DM Eurocheque
  328.           - C.O.D. (Cash-On-Delivery)
  329.           - upon invoice (only within Germany, net 14 days)
  330.  
  331.       If you have a European VAT ID, or if you order from outside the
  332.       European Union, you are exempt from the German VAT, but you may
  333.       have to pay your local VAT and/or import duties according to
  334.       local laws.
  335.  
  336.    b) International credit card or US-$ cheque payment is possible by
  337.       ordering through the following web-sites
  338.       (currently available only for CMATH for Borland C++):
  339.  
  340.       Atlantic Coast's SoftShop:
  341.       http://www.soft-shop.com/cgi-bin/order.html?136
  342.  
  343.       (this is the SoftShop sales page for all our products; please
  344.        be sure to choose the right one from the menu)
  345.  
  346.           $ 39 for  1 unit of the educational edition
  347.           $ 60 for  1 unit of the commercial edition,
  348.           $200 for  5 units,
  349.           $350 for 10 units
  350.           Add $5 for shipping&handling and applicable VAT.
  351.  
  352.  
  353.       ShareIt:
  354.       CMATH for Borland C++:
  355.           http://www.shareit.com/programs/101353.htm
  356.       CMATH for Microsoft Visual C++:
  357.           http://www.shareit.com/programs/103422.htm
  358.  
  359.           $ 44 per unit of the educational edition (including S&H)
  360.           $ 65 per unit of the commercial edition (including S&H).
  361.                Add applicable VAT.
  362.  
  363.       You may also order by e-mail to register@shareit.com.
  364.       US customers can also call 1-800-903-4152 (only for orders, please).
  365.       US check and cash orders can be sent to ShareIt!'s US office at
  366.            ShareIt! Inc.
  367.            P.O. Box 97841 
  368.            Pittsburgh, PA 15227-0241
  369.            USA
  370.       * When ordering by e-mail, phone, or postal mail through ShareIt,  *
  371.       * please have the program number ready:                            *
  372.       * CMATH for Borland C++:    No. 101353                             *
  373.       *     dto., educational:    No. 102655                             *
  374.       * CMATH for MSVC:           No. 103422                             *
  375.       *     dto., educational:    No. 103441                             *
  376.  
  377.  
  378. ****************************************************************************
  379. *                                                                          *
  380. *******                 2. Getting Started                           *******
  381. *                                                                          *
  382. ****************************************************************************
  383.  
  384. 2.1 Installation
  385. ----------------
  386. If you got CMATH as a part of OptiVec (a comprehensive library of
  387. vectorized functions by the same author), CMATH is automatically installed
  388. when you install OptiVec itself. In this case, installation of CMATH is
  389. already included and you should skip this chapter to continue with chapter 3.
  390.  
  391. Otherwise, please note the following points:
  392.  
  393. 1.  In order to use CMATH, you need an already installed copy of your
  394.     C/C++ compiler. Install CMATH by executing INSTALL.EXE on the
  395.     installation disk.
  396.  
  397. 2.  Add the installation directory (which you chose during installation) to
  398.     the library search path and to the include-file search path of the IDE
  399.     and of the configuration file TURBOC.CFG, in case you are using the
  400.     command-line compiler of Borland C++.
  401.  
  402. 3.  Borland C++:
  403.       Choose the desired platform (DOS, Windows3.x, or Win32).
  404.       If you chose DOS or Windows3.x, select the memory model LARGE.
  405.       For Win32, it is automatically FLAT. Choose static linking.
  406.       You should also choose, at least, 386 code generation and real
  407.       coprocessor commands (i.e., no emulation).
  408.  
  409.     Microsoft Visual C++:
  410.       Choose "single-thread debug" or "multi-thread debug".
  411.  
  412. 4.  Add the desired CMATH library to your project list.
  413.     Borland C++:
  414.         For DOS programs, this is the library CMATHL3.LIB,
  415.         for Windows3.x you need CMATHL3W.LIB,
  416.         for Win32, CMATHF3W.LIB.
  417.     Microsoft Visual C++:
  418.        The library needed is CMVCSD.LIB for single-thread debug,
  419.        or CMVCMTD.LIB for multi-thread debug.
  420.  
  421. 5.  In your C++ programs, declare 
  422.          #include <newcplx.h>
  423.     For C modules, declare:
  424.          #include <cmath.h>
  425.     If you are using ObjectWindows or MFC, <newcplx.h> or <cmath.h>
  426.     must be included after(!) the include files for OWL or MFC.
  427.  
  428. 6. Borland C++, 16-bit programs only:
  429.     *   If the linker option "process extended dictionaries" is available
  430.         in your version of Borland C++, you must switch it on.
  431.         Otherwise, you might get a "Table limit exceeded" linker error.
  432.  
  433.     *   CMATH works with Borland (Turbo) C++, version 3.0 or higher. Since,
  434.         from version 4.0 on, Borland changed the name of the error handling
  435.         routine matherr (without underbar) into _matherr (with a leading
  436.         underbar), any 16-bit program using CMATH has to call a macro,
  437.         NEWMATHERR, which takes  care of redirecting calls to _matherr,
  438.         if necessary. You should place the call to NEWMATHERR into the
  439.         module containing  main() or OwlMain():
  440.  
  441.              #include .....
  442.              #include <nexcplx.h>   /* or: #include <cmath.h> */
  443.              NEWMATHERR
  444.  
  445.              int main( void )
  446.              {    ..........   }
  447.  
  448.         If you forget to call NEWMATHERR, you will get a linker error
  449.         "Unresolved external _matherr" in the Borland C versions from 4.0 on.
  450.  
  451.         Inclusion of the macro NEWMATHERR is not needed for 32-bit programs.
  452.  
  453. After these preparations, all CMATH functions are available for your programs.
  454.  
  455.  
  456. 2.2 De-Installation
  457. -------------------
  458.  
  459. Should you wish to remove CMATH from your computer after testing, please
  460. simply delete the directory CMATH with its subdirectories. The installation
  461. of CMATH does not affect any files outside its own directory, so there
  462. is nothing else to get rid of.
  463.  
  464.  
  465.  
  466. ****************************************************************************
  467. *                                                                          *
  468. *******       3. Overview over the Functions of CMATH                *******
  469. *                                                                          *
  470. ****************************************************************************
  471.  
  472. For C++, all functions of CMATH are declared in <nexcplx.h>.
  473. For C, there are the following three include-files:
  474. <cfmath.h> declares all functions for single precision (data type fComplex),
  475. <cdmath.h> contains all double-precision functions (data type dComplex), and
  476. <cemath.h> lists all functions for extended precision (data type eComplex).
  477. The include-file <cmath.h> unites the three include-files just mentioned.
  478.  
  479. In the following, it is often only the fComplex-version of a function that
  480. is explicitly mentioned. The versions for dComplex and eComplex are always
  481. exactly analogous.
  482.  
  483. All functions for the language C have a prefix denoting the data type on
  484. which the function works:
  485. "cf_" stands for single precision (arguments and return values of the data
  486.       type fComplex, sometimes together with float),
  487. "cd_" stands for double precision (dComplex and double), whereas
  488. "ce_" denotes extended-precision functions.
  489.  
  490. In C++, synonyms are defined for all these functions. The synonyms do not
  491. have a prefix, since the data type information is implicitly handled by the
  492. C++ compiler. The C++ function names are always identical to those found
  493. in the complex class libraries (if the respective function exists there).
  494. Of course, if you wish to use the C function names in your C++ modules,
  495. you can do so by including <cmath.h> instead of <newcplx.h>.
  496.  
  497.  
  498. 3.1 Initialization of Complex Numbers
  499. -------------------------------------
  500.  
  501. For C++ modules, there are several overloaded constructors:
  502.           fComplex fComplex( float RePart, float ImPart );
  503.           fComplex fComplex( float RePart );  // imaginary part always 0
  504.           fComplex fComplex( dComplex );
  505.           fComplex fComplex( eComplex );
  506.  
  507.           The interconversion between the complex types of different
  508.           level of accuracy may, in the course of down-conversions,
  509.           lead to OVERFLOW errors. These are are caught and treated
  510.           via _matherr.
  511.           Similarly to the constructor fComplex(), also dComplex() and
  512.           eComplex() exist in overloaded versions performing the same
  513.           tasks for the classes dComplex and eComplex, respectively.
  514.  
  515. Additionally, both in C++ and in plain-C modules, complex numbers may
  516. be initialized by separately assigning a value to the imaginary and
  517. real parts, e.g.:
  518.         z.Re = 3.0;   z.Im = 5.7;
  519.  
  520. Alternatively, in plain-C modules, the same initialization can be
  521. accomplished by the function fcplx:
  522.     z = fcplx( 3.0, 5.7 );
  523.  
  524. For double-precision complex numbers, use dcplx, for extended-precision
  525. complex numbers, use ecplx.
  526.  
  527. Since overloading of functions and constructors is specific to C++ and
  528. not available in plain C, the interconversions between the various
  529. complex types are performed via the functions
  530. cftocd,  cdtocf,  cftoce,  cetocf,  cdtoce,  and cetocd.
  531. As described above, OVERFLOW errors in the course of down-conversions
  532. are caught and treated via _matherr.
  533.  
  534. 3.2 Basic Complex Operations
  535. ----------------------------
  536.  
  537. The following basic complex operations are defined in CMATH:
  538.  
  539. C function  C++ synonym
  540. cf_conj     conj          complex-conjugate form,
  541. cf_neg      neg  (or -)   negation,
  542. cf_real     real          extraction of the real part,
  543. cf_imag     imag          extraction of the imaginary part,
  544. cf_polar    polar         conversion of polar coordinates into the "normal",
  545.                           Cartesian complex format
  546. cf_abs      abs           absolute value (magnitude of the pointer in the
  547.                           complex plane; this is treated as a math function
  548.                           with error handling),
  549. cf_arg      arg           argument (angle of the pointer in the complex
  550.                           plane),
  551. cf_norm     norm          norm (defined here as the square of the absolute
  552.                           value)
  553.  
  554. (The cd_ and ce_ versions are exactly analogous to the cf_ version)
  555.  
  556.  
  557. 3.3 Arithmetic Operations
  558. -------------------------
  559.  
  560. Only C++: The following set of operators is available for all of
  561.           the three complex classes:
  562.           +  -  *  /  +=  -=  *=  /=   ==   !=
  563.           These operators exist also for "mixed" arguments,
  564.           where one argument is complex, the other real and
  565.           where the arguments are of different floating-point
  566.           accuracies.
  567.  
  568. Since it is only the language C++, but not C, which allows to overload
  569. the arithmetic operators, all arithmetic operations of complex
  570. numbers are implemented in a different way for C modules. Here, we
  571. have the functions
  572.  
  573. cf_add        addition of two complex numbers
  574. cf_addRe      addition of a complex number and a real number
  575. cf_sub        subtraction of two complex numbers (first operand
  576.               minus the second operand)
  577. cf_subRe      subtraction of a real number from a complex number
  578. cf_subrRe     subtraction of a complex number from a real number
  579. cf_mul        multiplication of two complex numbers
  580. cf_mulRe      multiplication of a complex number and a real number
  581. cf_div        division of two complex numbers (first operand
  582.               divided by the second operand)
  583. cf_divRe      division of a complex number by a real number
  584. cf_divrRe     division of a real number by a complex number
  585.  
  586. (similarly the cd_ and ce_ versions)
  587.  
  588. The equality operator "=" is the only operator defined also in C for
  589. complex numbers.
  590.  
  591.  
  592. 3.4 Mathematical Functions
  593. --------------------------
  594.  
  595. CMATH contains all mathematical functions you would find in the complex
  596. class libraries, along with several additional ones:
  597.  
  598. C function   C++ synonym
  599. cf_abs       abs            ry = | zx |          absolute value
  600. cf_acos      acos           zy = acos( zx )      arcus cosine function
  601. cf_asin      asin           zy = asin( zx )      arcus sine function
  602. cf_atan      atan           zy = atan( zx )      arcus tangent function
  603. cf_cos       cos            zy = cos( zx )       cosine
  604. cf_cosh      cosh           zy = cosh( zx )      hyperbolic cosine
  605. cf_cubic     cubic          zy = zx**3           third power
  606. cf_exp       exp            zy = exp( zx )       exponential function
  607. cf_inv       inv            zy = 1.0 / zx        inverse
  608. cf_ipow      ipow           zy = zx**n           integer power
  609. cf_ln        ln             zy = ln( zx )        natural logarithm
  610. cf_log       log            zy = ln( zx )        identical to cf_ln, ln
  611. cf_log2      log2           zy = lb( zx )        binary logarithm
  612. cf_log10     log10          zy = lg( zx )        decadic logarithm
  613. cf_pow       pow            zy = zx**zexp        arbitrary power
  614. cf_powReBase pow, powReBase zy = r**zx           real base to complex power
  615. cf_powReExpo pow, powReExpo zy = zx**r           real power of complex base
  616. cf_quartic   quartic        zy = zx**4           fourth power
  617. cf_sin       sin            zy = sin( zx )       sine
  618. cf_sinh      sinh           zy = sinh( zx )      hyperbolic sine
  619. cf_square    square         zy = zx²             square
  620. cf_sqrt      sqrt           zy = sqrt( zx )      square root
  621. cf_tan       tan            zy = tan( zx )       tangent
  622. cf_tanh      tanh           zy = tanh( zx )      hyperbolic tangent
  623.  
  624. (similarly the cd_ and ce_ versions)
  625.  
  626.  
  627. ****************************************************************************
  628. *                                                                          *
  629. *******                      4. Error Handling                       *******
  630. *                                                                          *
  631. *****************************************************************************
  632.  
  633. 4.1 General Error Handling of Complex Functions
  634. -----------------------------------------------
  635.  
  636. The error handling of complex functions follows the rules employed also
  637. for real-number functions and operations. For all arithmetic operations,
  638. the design of the algorithms eliminates the danger of failure due to
  639. irregular intermediate results. Overflowing or otherwise irregular
  640. final results, however, will lead to a hardware interrupt being generated
  641. and, as a consequence, to a program abort.
  642.  
  643. In contrast to the arithmetic operations, all mathematical functions and
  644. all data-type interconversions perform a tight error checking and treat
  645. any detected error conditions via _matherr (for fComplex and dComplex
  646. functions) and _matherrl (for eComplex functions). All error messages
  647. eventually generated use the C name (and not the C++ synonym) of the
  648. failing function.
  649.  
  650. 16-bit programs only:
  651.    As mentioned already in chapter 2, Borland has changed the name of the
  652.    error-handling function "matherr" (without leading underbar), used in
  653.    the versions 3.x, into "_matherr" (with a leading underbar), from
  654.    version 4.0 on.
  655.    In order to make CMATH compatible with both the older and the later
  656.    versions of Borland C++, the following way of error handling was adopted:
  657.    In case of an error, all CMATH functions call primarily matherr (as in the
  658.    older versions of Borland C++). A macro NEWMATHERR provides for the
  659.    necessary redirection of these calls to _matherr, if a later version of
  660.    Borland C++ is used. Therefore, NEWMATHERR must be called once (!) in any
  661.    program using CMATH, after the inclusion of <cmath.h>. The best place
  662.    is the module containing the main(), WinMain(), or OWLMain() function:
  663.  
  664.        #include <cmath.h>
  665.        #include ...
  666.        NEWMATHERR
  667.          ......
  668.        main()
  669.        { ... }
  670.  
  671.  
  672. If you use CMATH as a part of OptiVec, consult also chapter 5 of
  673. HANDBOOK.TXT for further information on floating-point error handling.
  674. If you use CMATH separately from OptiVec, you should at least know
  675. about some advanced error handling features, borrowed from VectorLib and
  676. described in the following chapter.
  677.  
  678.  
  679. 4.2 Advanced Error Handling: Writing Messages into a File
  680. ---------------------------------------------------------
  681.  
  682. ANSI C provides the user-definable function perror to print error messages.
  683. However, several compilers, including Borland C++, do not use perror for
  684. this purpose. This means that the way error messages are printed is not
  685. controllable by the programmer. While this is fine in most instances, there
  686. may be situations in which you might, for example, wish the error messages
  687. not to be printed to the screen, but rather into a file, so that you could
  688. check later what has gone wrong. If you use Borland C++ for Windows, an
  689. additional motivation could come from the fact that, for any error, a
  690. message box is displayed and program execution interrupted until you
  691. acknowledge having taken notice of the error.
  692.  
  693. You might wish to circumvent this. To this end, CMATH borrows from OptiVec
  694. the function  V_setErrorEventFile. This function needs as arguments the
  695. desired name of your event file and a switch named ScreenAndFile which
  696. decides if the error message is printed only into the file, or additionally
  697. to the screen as well.
  698. Example:     V_setErrorEventFile( "MyErrors.LOG", 1 );
  699.  
  700. Note that this redirection of error messages is valid only for errors
  701. occurring in CMATH (and, of course, VectorLib) routines. If you care to
  702. do so, however, there is a way to extend the redirection also to
  703. "non-CMATH/VectorLib" functions:  you may modify  _matherr and _matherrl
  704. so that the statement
  705.     return 0;
  706. (which signals an unresolved error)  is replaced by the sequence
  707.     V_noteError( e->name, e->type ); return 1;
  708.  
  709. Thereby the task of printing the error message for unresolved errors is
  710. passed to V_noteError. Keep in mind that it is the return value of _matherr
  711. which decides if an error message is printed by the default error handler
  712. of Borland C/C++. Thus, after the call to V_noteError, the printing of the
  713. default error messages is by-passed by returning "1". (Also, do not forget
  714. that it is your(!) _matherr routine which determines which errors you accept
  715. and which not.)
  716.  
  717. For example, your _matherr function (matherr - without the leading underbar
  718. - for Borland C++ 3.0 and 3.1) might look like the following one:
  719.  
  720.     #include <math.h>
  721.     int  _matherr( struct exception *e)
  722.     {
  723.         if( (e->type == UNDERFLOW) ││ (e->type == TLOSS) )  /* ignore */ ;
  724.         else   /* all other errors deserve at least notice */
  725.         {
  726.               V_noteError( e->name, e->type );
  727.               if (e->type == DOMAIN) exit(1); /* really fatal */
  728.         }
  729.         return 1;
  730.     }
  731.  
  732. (Of course, if you decide to change _matherr, do not forget to change
  733. _matherrl in the same way!).
  734.  
  735. The default printing of error messages on the screen alone is restored by
  736. V_closeErrorEventFile().
  737.  
  738.  
  739. ****************************************************************************
  740. *                                                                          *
  741. *******               5. Syntax Reference                            *******
  742. *                                                                          *
  743. ****************************************************************************
  744.  
  745. In the following, the syntax of all CMATH functions of float / fComplex
  746. accuracy is given. The syntax of the functions for double and extended
  747. precisions is exactly analogous.
  748. If you chose the "classic" class complex, this is also similar.
  749. Just replace "float" by "double" and "fComplex" by "complex".
  750. (Of course, the type-casting operators and the interconversion functions
  751. do not exist if there is only one type.)
  752.  
  753. 5.1 C++ Version
  754. ---------------
  755. At this place, the syntax is given in a simplified form (just what you need
  756. to know in order to use these functions). For details of the declaration,
  757. see the class complex<float> in <nexcplx.h>.
  758. The arithmetic operators are not listed; their use is identical to the use
  759. of the real-number operators; suffice it to say they are all available.
  760.  
  761.     float     abs( fComplex _z );
  762.     fComplex  acos( fComplex _z );
  763.     float     arg( fComplex _z );
  764.     fComplex  asin( fComplex _z );
  765.     fComplex  atan( fComplex _z );
  766.     fComplex  cdtocf( dComplex cd );
  767.     fComplex  cetocf( eComplex ce );
  768.     fComplex  conj( fComplex _z );
  769.     fComplex  cos( fComplex _z );
  770.     fComplex  cosh( fComplex _z );
  771.     fComplex  cubic( fComplex _z ); 
  772.     fComplex  exp( fComplex _z );
  773.     fComplex  fComplex( float Re_part, float Im_part );
  774.     fComplex  fComplex( float Re_part );  // constructors
  775.     fComplex  fComplex( dComplex cd );
  776.     fComplex  fComplex( eComplex ce );   // type-casting constructors
  777.     float     imag();   // to be used as   zim = z.imag();
  778.     float     imag( fComplex _z );
  779.     fComplex  inv( fComplex _z );   
  780.     fComplex  ipow( fComplex __base, int __expon ); 
  781.     fComplex  ln( fComplex _z );
  782.     fComplex  log( fComplex _z ); 
  783.     fComplex  log2( fComplex _z );
  784.     fComplex  log10( fComplex _z );
  785.     fComplex  neg( fComplex _z );
  786.     float     norm( fComplex _z );
  787.     fComplex  polar( float _mag, float _angle=0 );
  788.     fComplex  pow( fComplex __base, float __expon );
  789.     fComplex  powReExpo( fComplex __base, float __expon );
  790.     fComplex  pow( float __base, fComplex __expon);
  791.     fComplex  powReBase( float __base, fComplex __expon );
  792.     fComplex  pow( fComplex __base, fComplex __expon);
  793.     fComplex  quartic( fComplex _z ); 
  794.     float     z.real();     // to be used as    zre = z.real();
  795.     float     real( fComplex _z );
  796.     fComplex  sin( fComplex _z );
  797.     fComplex  sinh( fComplex _z );
  798.     fComplex  sqrt( fComplex _z );
  799.     fComplex  square( fComplex _z );
  800.     fComplex  tan( fComplex _z );
  801.     fComplex  tanh( fComplex _z );
  802.     
  803.  
  804. 5.2 Syntax Reference for the Plain-C Version.
  805. Again, the syntax is given in a simplified form here. For details of
  806. the declaration, see <cfmath.h>.
  807. The prefix cf_ denotes the accuracy level "float / fComplex".
  808. For the functions of double/dComplex and extended/eComplex precisions,
  809. the prefixes are cd_ and ce_, respectively.
  810. Since overloaded operators are specific to C++, only the operator "="
  811. exists for plain C.
  812.  
  813.     float     cf_abs(  fComplex __z );
  814.     fComplex  cf_acos( fComplex __z );
  815.     fComplex  cf_add(   fComplex __x, fComplex __y );
  816.     fComplex  cf_addRe( fComplex __x, float __yRe );
  817.     float     cf_arg(  fComplex __z );
  818.     fComplex  cf_asin( fComplex __z );
  819.     fComplex  cf_atan( fComplex __z );
  820.     fComplex     cdtocf( dComplex __zd );
  821.     fComplex     cetocf( eComplex __ze );
  822.     fComplex  cf_conj( fComplex __z );
  823.     fComplex  cf_cos(  fComplex __z );
  824.     fComplex  cf_cosh( fComplex __z );
  825.     fComplex  cf_cubic( fComplex __z );
  826.     fComplex  cf_div(   fComplex __x, fComplex __y );
  827.     fComplex  cf_divRe( fComplex __x, float __yRe );   /*  x / yRe  */
  828.     fComplex  cf_divrRe( fComplex __x, float __yRe );  /*  yRe / x  */
  829.     fComplex  cf_exp(  fComplex __z );
  830.     fComplex  fcplx( float __ReVal, float __ImVal);
  831.     float     cf_imag( fComplex z );
  832.     fComplex  cf_inv(  fComplex __z );    /*   1.0 / z   */
  833.     fComplex  cf_ipow( fComplex __z, int __exponent );
  834.     fComplex  cf_ln(    fComplex __z );
  835.     fComplex  cf_log(   fComplex __z ); 
  836.     fComplex  cf_log2(  fComplex __z );
  837.     fComplex  cf_log10( fComplex __z );
  838.     fComplex  cf_mul(   fComplex __x, fComplex __y );
  839.     fComplex  cf_mulRe( fComplex __x, float __yRe );
  840.     fComplex  cf_neg(  fComplex __z );
  841.     float     cf_norm( fComplex __z );
  842.     fComplex  cf_polar( float __mag, float __angle );
  843.     fComplex  cf_pow( fComplex __base, fComplex __exponent );
  844.     fComplex  cf_powReBase( float __base, fComplex __exponent ); 
  845.     fComplex  cf_powReExpo( fComplex __base, float __exponent );
  846.     fComplex  cf_quartic( fComplex __z );  
  847.     float     cf_real( fComplex z );
  848.     fComplex  cf_sin(  fComplex __z );
  849.     fComplex  cf_sinh( fComplex __z );
  850.     fComplex  cf_square( fComplex __z );
  851.     fComplex  cf_sqrt( fComplex __z );
  852.     fComplex  cf_sub(   fComplex __x, fComplex __y );
  853.     fComplex  cf_subRe( fComplex __x, float __yRe );  /* x - yRe */
  854.     fComplex  cf_subrRe( fComplex __x, float __yRe ); /* yRe - x */
  855.     fComplex  cf_tan(  fComplex __z );
  856.     fComplex  cf_tanh( fComplex __z );
  857.  
  858.  
  859. *****************************************************************************
  860.  
  861. Although CMATH underwent thorough testing, there is always a possibility
  862. that a problem might have escaped our attention. Should you feel you dis-
  863. covered a "bug", please kindly try to specify the conditions under which
  864. you observed the problem as exactly as possible. Please let the author
  865. know what you have found!
  866. e-mail:  MartinSander@Bigfoot.com
  867.  
  868. Copyright (C) Martin Sander 1996-1999
  869.  
  870. *****************************************************************************
  871.